1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=utf-8" />
6 <title>BareMetal OS - System Calls
</title>
7 <style type=
"text/css">
8 body
{color:#222; font-family:Verdana
,sans-serif
; font-size:12px;}
9 pre
{background-color:#FAFAFA; border:1px solid
#DADADA; margin-bottom:6px; padding:6px;}
11 <script type=
"text/javascript" src=
"scripts/shCore.js"></script>
12 <script type=
"text/javascript" src=
"scripts/shBrushAsm.js"></script>
13 <script type=
"text/javascript" src=
"scripts/shBrushCpp.js"></script>
14 <link type=
"text/css" rel=
"stylesheet" href=
"css/shCore.css"/>
15 <link type=
"text/css" rel=
"stylesheet" href=
"css/shThemeDefault.css"/>
16 <script type=
"text/javascript">
17 SyntaxHighlighter
.defaults
['tab-size'] = 8;
18 SyntaxHighlighter
.all();
23 <div class=
"container">
25 <h1>BareMetal OS - System Calls
</h1>
27 <h3>Version
0.5.2 (DRAFT),
4 July
2011 - Return Infinity
</h3>
29 <p>This documentation details the system calls provided by BareMetal and how applications can use them.
</p>
34 <hr noshade=
"noshade"></hr>
41 <p>The BareMetal OS kernel includes system calls for outputting to the screen, taking keyboard input, manipulating strings, producing PC speaker sounds, math operations and disk I/O. You can get a quick reference to the registers that these calls take and pass back by looking at
<strong>bmdev.asm
</strong>, and see practical use of the calls in the
<strong>programs/
</strong> directory.
</p>
43 <p>Here we have a detailed API explanation with examples. You can see the full source code behind these system calls in the
<strong>os/syscalls/
</strong> directory in BareMetal OS. Each aspect of the API below is contained within their respective assembly source files.
</p>
46 <h3>Table of contents
</h3>
48 <li><a href=
"#debug">Debug
</a></li>
49 <li><a href=
"#file">File I/O
</a></li>
50 <li><a href=
"#input">Input
</a></li>
51 <li><a href=
"#math">Mathematics
</a></li>
52 <li><a href=
"#memory">Memory
</a></li>
53 <li><a href=
"#misc">Miscellaneous
</a></li>
54 <li><a href=
"#network">Networking
</a></li>
55 <li><a href=
"#output">Output
</a></li>
56 <li><a href=
"#serial">Serial
</a></li>
57 <li><a href=
"#smp">SMP (Symmetric Multiprocessing)
</a></li>
58 <li><a href=
"#sound">Sound
</a></li>
59 <li><a href=
"#string">String
</a></li>
67 <hr noshade=
"noshade" style=
"width:80%;"></hr>
74 <h3>b_debug_dump_reg
</h3>
75 <p><strong>Dump the values on the registers to the screen
</strong></p>
78 OUT: Nothing, all registers preserved
81 <pre class=
"brush: asm; tab-size: 8">
88 <h3>b_debug_dump_mem
</h3>
89 <p><strong>Dump some memory content to the screen
</strong></p>
91 IN: RSI = Start of memory address to dump
92 RCX = number of bytes to dump
93 OUT: Nothing, all registers preserved
96 <pre class=
"brush: asm; tab-size: 8">
97 mov rsi,
0x1000 ; Dump starting at this memory address
98 mov rcx,
100 ; Display
100 bytes
105 <h3>b_debug_dump_rax|eax|ax|al
</h3>
106 <p><strong>Dump content of RAX, EAX, AX, or AL to the screen in hex format
</strong></p>
108 IN: RAX|EAX|AX|AL = content to dump
109 OUT: Nothing, all registers preserved
112 <pre class=
"brush: asm; tab-size: 8">
113 call b_debug_dump_rax ; Dump the entire
64-bit register
114 call b_debug_dump_eax ; Dump only the lower
32-bits
115 call b_debug_dump_ax ; Dump only the lower
16-bits
116 call b_debug_dump_al ; Dump only the lower
8-bits
122 <h3>b_debug_get_ip
</h3>
123 <p><strong>Dump content of RIP into RAX
</strong></p>
128 <p>This function is useful for retreiving the Instruction Pointer at the time b_debug_get_ip was called.
</p>
130 <pre class=
"brush: asm; tab-size: 8">
139 <hr noshade=
"noshade" style=
"width:80%;"></hr>
141 <h2>File I/O - Functions for dealing with files
</h2>
147 <p><strong>Read a file from disk into memory
</strong></p>
149 IN: RSI = Address of filename string
150 RDI = Memory location where file will be loaded to
151 OUT: Carry is set if the file was not found or an error occured
155 <pre class=
"brush: asm; tab-size: 8">
156 mov rsi, testfile ; File name
157 mov rdi,
0x1000000 ; Read file to this memory address
159 jnc fileok ; If carry is clear then no error occured
161 ... error handling here ...
164 ... work with file ...
166 testfile: db
"test.txt",
0
172 <h3>b_file_write
</h3>
173 <p><strong>Write memory to a file on disk
</strong></p>
175 IN: RSI = Memory location of data to be written
176 RDI = Address of filename string
177 RCX = Number of bytes to write
178 OUT: Carry is set if an error occured
182 <pre class=
"brush: asm; tab-size: 8">
183 mov rdi, testfile ; File name
184 mov rsi,
0x100A000 ; Data to write is at this memory address
185 mov rcx,
50 ; Write
50 bytes
187 jnc fileok ; If carry is clear then no error occured
189 ... error handling here ...
194 testfile: db
"test.txt",
0
200 <h3>b_file_delete
</h3>
201 <p><strong>Delete a file from disk
</strong></p>
203 IN: RSI = Memory location of file name to delete
204 OUT: Carry is set if the file was not found or an error occured
208 <pre class=
"brush: asm; tab-size: 8">
209 mov rsi, testfile ; Name of file to delete
214 testfile: db
"deleteme.txt",
0
220 <h3>b_file_get_list
</h3>
221 <p><strong>Generate a list of files on disk
</strong></p>
223 IN: RDI = location to store list
224 OUT: RDI = pointer to end of list
228 <pre class=
"brush: asm; tab-size: 8">
229 mov rdi, temp_string ; Our temporary string
230 mov rsi, rdi ; Point RSI to the start of the string
232 call b_print_string ; Print the directory listing
238 <h3>b_file_get_size
</h3>
239 <p><strong>Return the size of a file on disk
</strong></p>
241 IN: RSI = Address of filename string
242 OUT: RCX = Size in bytes
243 Carry is set if the file was not found or an error occured
247 <pre class=
"brush: asm; tab-size: 8">
248 mov rsi, temp_string ; Our temporary name string
258 <hr noshade=
"noshade" style=
"width:80%;"></hr>
260 <h2>Input - Functions for dealing with input from the keyboard
</h2>
265 <h3>b_input_key_check
</h3>
266 <p><strong>Scans keyboard for input, but doesn't wait
</strong></p>
269 OUT: AL =
0 if no key pressed, otherwise ASCII code, other regs preserved
270 Carry flag is set if there was a keystoke, clear if there was not
271 All other registers preserved
274 <pre class=
"brush: asm; tab-size: 8">
275 call b_input_key_check
276 jnc nokeypressed ; If carry is not set than no key was pressed
277 call b_print_char ; Print the character that was pressed
287 <h3>b_input_key_wait
</h3>
288 <p><strong>Waits for keypress and returns key
</strong></p>
291 OUT: AL = key pressed
292 All other registers preserved
295 <pre class=
"brush: asm; tab-size: 8">
296 call b_input_key_wait
297 cmp al, 'q' ; Check if Q key was hit
299 cmp al, 'w' ; Check if W key was hit
310 <h3>b_input_string
</h3>
311 <p><strong>Take string from keyboard entry
</strong></p>
313 IN: RDI = location where string will be stored
314 RCX = number of characters to accept
315 OUT: RCX = length of string that was inputed (NULL not counted)
316 All other registers preserved
318 <p>Make sure that RCX is set to a sane value as it will stop memory from being corrupted.
</p>
320 <pre class=
"brush: asm; tab-size: 8">
327 inputstring: times
11 db
0
335 <hr noshade=
"noshade" style=
"width:80%;"></hr>
343 <p><strong>Add two
128-bit integer together
</strong></p>
345 IN: RDX,RAX = Integer
1, RCX,RBX = Integer
2
346 OUT: RDX,RAX = Result
347 Carry set if overflow
350 <pre class=
"brush: asm; tab-size: 8">
359 <hr noshade=
"noshade" style=
"width:80%;"></hr>
360 <a name=
"memory"></a>
366 <h3>b_mem_allocate
</h3>
367 <p><strong>Allocates the requested number of
2 MiB pages
</strong></p>
369 IN: RCX = Number of pages to allocate
370 OUT: RAX = Starting address
371 RCX = Number of pages allocated (Set to the value asked for or
0 on failure)
373 This function will only allocate continous pages
375 <pre class=
"brush: asm; tab-size: 8">
376 mov rcx,
4 ; Try to allocate
4 pages (
8 MiB)
380 mov [mempointer], rax ; Save the pointer
390 <h3>b_mem_release
</h3>
391 <p><strong>Frees the requested number of
2 MiB pages
</strong></p>
393 IN: RAX = Starting address
394 RCX = Number of pages to free
395 OUT: RCX = Number of pages freed
398 <pre class=
"brush: asm; tab-size: 8">
399 mov rax, [mempointer] ; Restore the pointer
400 mov rcx,
4 ; Free
4 pages (
8 MiB)
407 <h3>b_mem_get_free
</h3>
408 <p><strong>Returns the number of
2 MiB pages that are available
</strong></p>
411 OUT: RCX = Number of free
2 MiB pages
414 <pre class=
"brush: asm; tab-size: 8">
423 <hr noshade=
"noshade" style=
"width:80%;"></hr>
431 <p><strong>Delay by X
</strong></p>
433 IN: RCX = Time in eights of a second
434 OUT: All registers preserved
436 <p>A value of
8 in RCX will delay
1 second and a value of
1 will delay
1/
8 of a second. This function depends on the RTC (IRQ
8) so interrupts must be enabled.
</p>
438 <pre class=
"brush: asm; tab-size: 8">
440 call b_delay ; Pause for
2 seconds
448 <hr noshade=
"noshade" style=
"width:80%;"></hr>
449 <a name=
"network"></a>
455 <h3>b_ethernet_avail
</h3>
456 <p><strong>Check if Ethernet is available
</strong></p>
459 OUT: RAX = MAC Address if Ethernet is enabled, otherwise
0. All other registers preserved
462 <pre class=
"brush: asm; tab-size: 8">
463 call b_ethernet_avail
471 <h3>b_ethernet_tx
</h3>
472 <p><strong>Transmit a packet via Ethernet
</strong></p>
474 IN: RSI = Memory location where data is stored
475 RDI = Pointer to
48 bit destination address
476 BX = Type of packet (If set to
0 then the EtherType will be set to the length of data)
478 OUT: Nothing. All registers preserved
481 <pre class=
"brush: asm; tab-size: 8">
482 mov rsi, datalocation
492 <h3>b_ethernet_rx
</h3>
493 <p><strong>Polls the Ethernet card for received data
</strong></p>
495 IN: RDI = Memory location where packet will be stored
496 OUT: RCX = Length of packet
497 All other registers preserved
500 <pre class=
"brush: asm; tab-size: 8">
501 mov rdi, EthernetBuffer
512 <hr noshade=
"noshade" style=
"width:80%;"></hr>
513 <a name=
"output"></a>
514 <h2>Output - Functions for dealing with output to the screen
</h2>
519 <h3>b_move_cursor
</h3>
520 <p><strong>Moves cursor in text mode
</strong></p>
524 OUT: All registers preserved
526 <p>In normal text mode the screen is
80x25. First row and column are both
0. Max row is
24 and max column is
79.
</p>
528 <pre class=
"brush: asm; tab-size: 8">
531 call b_move_cursor ; Move cursor to (
5,
5)
536 call b_move_cursor ; Same as above but we set AL and AH at the same time
542 <h3>b_inc_cursor
</h3>
543 <p><strong>Increment the hardware cursor by one
</strong></p>
546 OUT: All registers preserved
548 <p>This function will automatically call b_scroll_screen if required.
</p>
550 <pre class=
"brush: asm; tab-size: 8">
557 <h3>b_dec_cursor
</h3>
558 <p><strong>Decrement the hardware cursor by one
</strong></p>
561 OUT: All registers preserved
564 <pre class=
"brush: asm; tab-size: 8">
571 <h3>b_print_newline
</h3>
572 <p><strong>Reset cursor to start of next line and scroll if needed
</strong></p>
575 OUT: All registers perserved
578 <pre class=
"brush: asm; tab-size: 8">
585 <h3>b_print_string
</h3>
586 <p><strong>Displays text
</strong></p>
588 IN: RSI = message location (zero-terminated string)
589 OUT: All registers perserved
591 <p>This function defaults to Light Gray text on a Black background
</p>
593 <pre class=
"brush: asm; tab-size: 8">
599 teststring: db
"This is a test string.",
0
605 <h3>b_print_string_with_color
</h3>
606 <p><strong>Displays text with color
</strong></p>
608 IN: RSI = message location (zero-terminated string)
610 OUT: All registers perserved
612 <p>The high
4 bits of BL is for the background color and the low
4 bits are for the text color. See the
<a href=
"#colortable">Color Chart
</a> in the Appendix.
</p>
614 <pre class=
"brush: asm; tab-size: 8">
615 mov rsi, colorteststring
616 mov bl,
0x04 ; Black background, Red text
617 call b_print_string_with_color
621 colorteststring: db
"This is a test string in color!",
0
627 <h3>b_print_char
</h3>
628 <p><strong>Displays a char
</strong></p>
630 IN: AL = char to display
631 OUT: All registers perserved
633 <p>This function defaults to Light Gray text on a Black background
</p>
635 <pre class=
"brush: asm; tab-size: 8">
643 <h3>b_print_char_with_color
</h3>
644 <p><strong>Displays a char with color
</strong></p>
646 IN: AL = char to display
648 OUT: All registers perserved
650 <p>The high
4 bits of BL is for the background color and the low
4 bits are for the text color. See the
<a href=
"#colortable">Color Chart
</a> in the Appendix.
</p>
652 <pre class=
"brush: asm; tab-size: 8">
654 mov bl,
0x4F ; Red background, White text
655 call b_print_char_with_color
661 <h3>b_print_char_hex
</h3>
662 <p><strong>Displays a char in hex mode
</strong></p>
664 IN: AL = char to display
665 OUT: All registers perserved
668 <pre class=
"brush: asm; tab-size: 8">
670 call b_print_char_hex ;
"5F" will be printed
672 call b_print_char_hex ;
"9A" will be printed
678 <h3>b_print_char_hex_with_color
</h3>
679 <p><strong>Displays a char in hex mode
</strong></p>
680 <p>The high
4 bits of BL is for the background color and the low
4 bits are for the text color. See the
<a href=
"#colortable">Color Chart
</a> in the Appendix.
</p>
682 IN: AL = char to display
684 OUT: All registers perserved
687 <pre class=
"brush: asm; tab-size: 8">
689 mov bl,
0x4F ; Red background, White text
690 call b_print_char_hex ;
"5F" will be printed
692 mov bl,
0x4F ; Red background, White text
693 call b_print_char_hex ;
"9A" will be printed
699 <h3>b_scroll_screen
</h3>
700 <p><strong>Scrolls the screen up by one line
</strong></p>
703 OUT: All registers perserved
706 <pre class=
"brush: asm; tab-size: 8">
713 <h3>b_hide_cursor
</h3>
714 <p><strong>Turns off cursor in text mode
</strong></p>
717 OUT: All registers perserved
720 <pre class=
"brush: asm; tab-size: 8">
727 <h3>b_show_cursor
</h3>
728 <p><strong>Turns on cursor in text mode
</strong></p>
731 OUT: All registers perserved
734 <pre class=
"brush: asm; tab-size: 8">
743 <hr noshade=
"noshade" style=
"width:80%;"></hr>
744 <a name=
"serial"></a>
750 <h3>b_serial_send
</h3>
751 <p><strong>Send a byte over the primary serial port
</strong></p>
753 IN: AL = Byte to send over serial port
754 OUT: All registers preserved
757 <pre class=
"brush: asm; tab-size: 8">
765 <h3>b_serial_recv
</h3>
766 <p><strong>Receive a byte from the primary serial port
</strong></p>
769 OUT: AL = Byte recevied
770 Carry flag is set if a byte was received, otherwise AL is trashed
771 All other registers preserved
774 <pre class=
"brush: asm; tab-size: 8">
784 <hr noshade=
"noshade" style=
"width:80%;"></hr>
786 <h2>Symmetric Multiprocessing (SMP)
</h2>
792 <p><strong>Resets a CPU/Core
</strong></p>
794 IN: AL = CPU APIC ID #
795 OUT: Nothing. All registers preserved.
797 <p>This function resets an AP to run the ap_clear kernel code.
</p>
799 <pre class=
"brush: asm; tab-size: 8">
800 mov al,
0x01 ; Select CPU Core with APIC ID
1
801 call b_smp_reset ; Reset the CPU Core
807 <h3>b_smp_get_id
</h3>
808 <p><strong>Returns the APIC ID of the CPU that ran this function
</strong></p>
811 OUT: RAX = CPU's APIC ID number
812 All other registers perserved
815 <pre class=
"brush: asm; tab-size: 8">
816 call b_smp_get_id ; APIC ID returned in AL
822 <h3>b_smp_enqueue
</h3>
823 <p><strong>Add a workload to the processing queue
</strong></p>
825 IN: RAX = Code to execute
826 OUT: Carry set on failure (Queue full)
827 All other registers perserved
829 <p>This function adds a workload to the processing queue of the system. All available CPU Cores actively poll this queue and will automatically execute b_smp_dequeue.
</p>
831 <pre class=
"brush: asm; tab-size: 8">
832 mov rax, ap_print_id ; Our code to run on all CPUs
833 mov rcx,
64 ; Number of instances to spawn
845 <h3>b_smp_dequeue
</h3>
846 <p><strong>Dequeue a workload from the processing queue
</strong></p>
849 OUT: RAX = Code to execute (Set to
0 if queue was empty)
850 All other registers perserved
852 <p>This function is automatically called by each CPU Core that is in the Idle Kernel loop. It is also useful for the BSP to call this function after adding the initial workloads to the queue.
</p>
854 <pre class=
"brush: asm; tab-size: 8">
856 call b_smp_dequeue ; Try to dequeue a workload
857 jc emptyqueue ; If carry is set then the queue is empty
858 call rax ; Otherwise run the workload
859 call b_smp_queuelen ; Check the length of the queue
861 jne bsp ; If it is not empty try to do another workload
868 <p><strong>Call the code address stored in RAX
</strong></p>
870 IN: RAX = Address of code to execute
873 <p>Needed for compatibilty with C.
</p>
875 <pre class=
"brush: asm; tab-size: 8">
877 call b_smp_run ; You could also use
"call rax" here
883 <h3>b_smp_queuelen
</h3>
884 <p><strong>Returns the number of items in the processing queue
</strong></p>
887 OUT: RAX = number of items in processing queue
889 <p>b_smp_dequeue makes use of this function.
</p>
891 <pre class=
"brush: asm; tab-size: 8">
892 call b_smp_queuelen ; Check the length of the queue
901 <p><strong>Wait until all other CPU Cores are finished processing
</strong></p>
904 OUT: All registers preserved
906 <p>Use this function to wait until all available CPU Cores are finished processing.
</p>
908 <pre class=
"brush: asm; tab-size: 8">
909 call b_smp_wait ; Wait for all other processors to finish
916 <p><strong>Attempt to lock a mutex
</strong></p>
918 IN: RAX = Address of lock variable
919 OUT: Nothing. All registers preserved.
921 <p>Attempt to lock a mutex. This function will block until the mutex can be locked.
</p>
923 <pre class=
"brush: asm; tab-size: 8">
925 call b_smp_lock ; Attempt to lock the mutex
931 <h3>b_smp_unlock
</h3>
932 <p><strong>Unlock a mutex
</strong></p>
934 IN: RAX = Address of lock variable
935 OUT: Nothing. All registers preserved.
937 <p>Unlock a mutex.
</p>
939 <pre class=
"brush: asm; tab-size: 8">
941 call b_smp_unlock ; Unlock the mutex
947 <h3>b_smp_numcores
</h3>
948 <p><strong>Returns the number of cores in this computer
</strong></p>
950 IN: RAX = Address of lock variable
951 OUT: Nothing. All registers preserved.
954 <pre class=
"brush: asm; tab-size: 8">
963 <hr noshade=
"noshade" style=
"width:80%;"></hr>
970 <h3>b_speaker_tone
</h3>
971 <p><strong>Generate a tone on the PC speaker
</strong></p>
973 IN: RAX = note frequency
974 OUT: All registers preserved
976 <p>Call b_speaker_off to stop the tone
</p>
978 <pre class=
"brush: asm; tab-size: 8">
979 mov rax,
4000 ; A 'C' note
986 <h3>b_speaker_off
</h3>
987 <p><strong>Turn off PC speaker
</strong></p>
990 OUT: All registers preserved
993 <pre class=
"brush: asm; tab-size: 8">
1000 <h3>b_speaker_beep
</h3>
1001 <p><strong>Create a standard OS beep
</strong></p>
1004 OUT: All registers preserved
1007 <pre class=
"brush: asm; tab-size: 8">
1016 <hr noshade=
"noshade" style=
"width:80%;"></hr>
1017 <a name=
"string"></a>
1023 <h3>b_int_to_string
</h3>
1024 <p><strong>Convert a binary interger into an string
</strong></p>
1026 IN: RAX = binary integer
1027 RDI = location to store string
1028 OUT: RDI = points to end of string
1029 All other registers preserved
1031 <p>The minimum return value is
0 and maximum return value is
18446744073709551615. The destination string needs to be able to store at least
21 characters (
20 for the digits and
1 for the string terminator).
</p>
1033 <pre class=
"brush: asm; tab-size: 8">
1034 mov rax,
0xFFFFFFFFFFFFFFFF
1036 mov rsi, rdi ; Set RSI for print_string call
1037 call b_int_to_string
1038 call b_print_string ;
"18446744073709551615" will be printed
1042 teststring: times
21 db
0 ; Space for a
20 character string
1048 <h3>b_string_to_int
</h3>
1049 <p><strong>Convert a string into a binary interger
</strong></p>
1051 IN: RSI = location of string
1052 OUT: RAX = integer value
1053 All other registers preserved
1056 <pre class=
"brush: asm; tab-size: 8">
1058 call b_string_to_int ; RAX will be set to the decimal value
460341242
1062 teststring: db
"460341242",
0
1069 <h3>b_int_to_hex_string
</h3>
1070 <p><strong>Convert an integer to a hex string
</strong></p>
1072 IN: RAX = Integer value
1073 RDI = Location to store string
1074 OUT: All registers preserved
1077 <pre class=
"brush: asm; tab-size: 8">
1080 mov rsi, rdi ; Set RSI for print_string call
1081 call b_int_to_hex_string
1082 call b_print_string ;
"DEADBEEF" will be printed
1086 teststring: times
21 db
0 ; Space for a
20 character string
1092 <h3>b_hex_string_to_int
</h3>
1093 <p><strong>Convert up to
8 hexascii to bin
</strong></p>
1095 IN: RSI = Location of hex asciiz string
1096 OUT: RAX = Binary value of hex string
1097 All other registers preserved
1100 <pre class=
"brush: asm; tab-size: 8">
1102 call b_hex_string_to_int ; RAX will be set to
4277006349
1106 teststring: db
"FEEDF00D",
0
1112 <h3>b_string_length
</h3>
1113 <p><strong>Return length of a string
</strong></p>
1115 IN: RSI = string location
1116 OUT: RCX = length (not including the NULL terminator)
1117 All other registers preserved
1120 <pre class=
"brush: asm; tab-size: 8">
1122 call b_string_length ; RCX will be set to
45
1126 teststring: db
"This is a test of the string length function!",
0
1132 <h3>b_find_char_in_string
</h3>
1133 <p><strong>Find first location of character in a string
</strong></p>
1135 IN: RSI = string location
1136 AL = character to find
1137 OUT: RAX = location in string, or
0 if char not present
1138 All other registers preserved
1141 <pre class=
"brush: asm; tab-size: 8">
1144 call b_find_char_in_string ; RAX will be set to
26 (The 'a' in 'lowercase' is the first instance)
1148 teststring: db
"Where is the first lowercase 'a'?",
0
1154 <h3>b_string_charchange
</h3>
1155 <p><strong>Change all instances of a character in a string
</strong></p>
1157 IN: RSI = string location
1158 AL = character to replace
1159 BL = replacement character
1160 OUT: All registers preserved
1163 <pre class=
"brush: asm; tab-size: 8">
1167 call b_string_charchange
1168 call b_print_string ;
"PlAasA rAplacA thA lowArcasA lAttAr E's." will be printed
1172 teststring: db
"Please replace the lowercase letter E's.",
0
1178 <h3>b_string_copy
</h3>
1179 <p><strong>Copy the contents of one string into another
</strong></p>
1183 OUT: All registers preserved
1185 <p>It is up to the programmer to ensure that there is sufficient space in the destination!
</p>
1187 <pre class=
"brush: asm; tab-size: 8">
1192 call b_print_string ;
"Copy this string!" will be printed
1196 srcstring:
"Copy this string!",
0
1197 deststring: times
21 db
0 ; Space for a
20 character string
1203 <h3>b_string_truncate
</h3>
1204 <p><strong>Chop string down to specified number of characters
</strong></p>
1206 IN: RSI = string location
1207 RAX = number of characters
1208 OUT: All registers preserved
1211 <pre class=
"brush: asm; tab-size: 8">
1214 call b_string_truncate
1215 call b_print_string ;
"AaBbC" will be printed on screen
1219 teststring: db
"AaBbCcDd 123",
0
1225 <h3>b_string_join
</h3>
1226 <p><strong>Join two strings into a third string
</strong></p>
1228 IN: RAX = string one
1230 RDI = destination string
1231 OUT: All registers preserved
1234 <pre class=
"brush: asm; tab-size: 8">
1240 call b_print_string ;
"AAAAABBBBB" will be printed
1246 deststring: times
41 db
0 ; Space for a
40 character string
1252 <h3>b_string_chomp
</h3>
1253 <p><strong>Strip leading and trailing spaces from a string
</strong></p>
1255 IN: RSI = string location
1256 OUT: All registers preserved
1259 <pre class=
"brush: asm; tab-size: 8">
1262 call b_print_string ;
"This is a test." will be printed
1266 teststring: db
" This is a test. ",
0
1273 <h3>b_string_strip
</h3>
1274 <p><strong>Removes specified character from a string
</strong></p>
1276 IN: RSI = string location
1277 AL = character to remove
1278 OUT: All registers preserved
1281 <pre class=
"brush: asm; tab-size: 8">
1285 call b_print_string ;
"Ths s a test of the strng_strp functon." will be printed
1289 teststring: db
"This is a test of the string_strip function.",
0
1295 <h3>b_string_compare
</h3>
1296 <p><strong>See if two strings match
</strong></p>
1298 IN: RSI = string one
1300 OUT: Carry flag set if same
1303 <pre class=
"brush: asm; tab-size: 8">
1306 call b_string_compare
1308 ; Falls through to here if strings are not equal
1319 <h3>b_string_uppercase
</h3>
1320 <p><strong>Convert zero-terminated string to uppercase
</strong></p>
1322 IN: RSI = string location
1323 OUT: All registers preserved
1326 <pre class=
"brush: asm; tab-size: 8">
1328 call b_string_uppercase
1329 call b_print_string ;
"AABBCCDD 123" will be printed
1333 teststring: db
"AaBbCcDd 123",
0
1339 <h3>b_string_lowercase
</h3>
1340 <p><strong>Convert zero-terminated string to lowercase
</strong></p>
1342 IN: RSI = string location
1343 OUT: All registers preserved
1346 <pre class=
"brush: asm; tab-size: 8">
1348 call b_string_lowercase
1349 call b_print_string ;
"aabbccdd 123" will be printed
1353 teststring: db
"AaBbCcDd 123",
0
1359 <h3>b_get_time_string
</h3>
1360 <p><strong>Store the current time in a string in format
"HH:MM:SS"</strong></p>
1362 IN: RDI = location to store string (must be able to fit
9 bytes,
8 data plus null terminator)
1363 OUT: All registers preserved
1366 <pre class=
"brush: asm; tab-size: 8">
1367 mov rdi, temp_string
1368 call b_get_time_string ; temp_string will be set to
"16:45:20" if that was the current time
1372 temp_string: times
41 db
0 ; Space for a
40 character string
1378 <h3>b_get_date_string
</h3>
1379 <p><strong>Store the current time in a string in format
"YYYY/MM/DD"</strong></p>
1381 IN: RDI = location to store string (must be able to fit
11 bytes,
10 data plus null terminator)
1382 OUT: All registers preserved
1385 <pre class=
"brush: asm; tab-size: 8">
1386 mov rdi, temp_string
1387 call b_get_date_string ; temp_string will be set to
"2010/04/27" if that was the current date
1391 temp_string: times
41 db
0 ; Space for a
40 character string
1398 <p><strong>Check if character is a digit
</strong></p>
1401 OUT: EQ flag set if numeric
1403 <p>JE (Jump if Equal) or JNE (Jump if Not Equal) can be used after this function is called.
</p>
1405 <pre class=
"brush: asm; tab-size: 8">
1408 je itsanumber ; Jump if it was a digit, else fall through to next instruction
1416 <p><strong>Check if character is a letter
</strong></p>
1419 OUT: EQ flag set if alpha
1421 <p>JE (Jump if Equal) or JNE (Jump if Not Equal) can be used after this function is called.
</p>
1423 <pre class=
"brush: asm; tab-size: 8">
1426 jne notaletter ; Jump if it was not a letter, else fall through to next instruction
1433 <h3>b_string_parse
</h3>
1434 <p><strong>Parse a string into individual words
</strong></p>
1436 IN: RSI = Address of string
1437 OUT: RCX = word count
1439 <p>This function will remove extra whitespace in the source string and also return the word count.
"This is a test. " will update to
"This is a test."</p>
1441 <pre class=
"brush: asm; tab-size: 8">
1444 call b_print_string ;
"This is a test." will be printed. Also RCX will be set to
4
1448 teststring: db
"This is a test. ",
0
1454 <h3>b_string_append
</h3>
1455 <p><strong>Append a string to an existing string
</strong></p>
1457 IN: RSI = String to be appended
1458 RDI = Destination string
1459 OUT: All registers preserved
1461 <p>Note: It is up to the programmer to ensure that there is sufficient space in the destination
</p>
1463 <pre class=
"brush: asm; tab-size: 8">
1464 mov rsi, teststring1
1465 mov rdi, teststring2
1466 call b_string_append
1467 mov rsi, teststring2
1468 call b_print_string ;
"Test2Test1" will be printed.
1472 teststring1: db
"Test1",
0
1473 teststring2: db
"Test2",
0
1481 <hr noshade=
"noshade"></hr>
1485 <a name=
"appendix"></a>
1487 <h3>Text mode color table:
</h3>
1488 <a name=
"colortable"></a>
1489 <img src=
"images/Text Mode Color Table.png"></img>
1490 <p>For the b_print_string_with_color and b_print_char_with_color the color for the foreground (text) and background is selected with BL. A value of
0x09 in BL would give bright blue text on a black background. A value of
0xA4 would give you red text on a bright green background (yuck).
</p>
1499 <hr noshade=
"noshade"></hr>
1504 <a name=
"license"></a>
1507 <p>BareMetal OS is open source and released under the
3-clause
"New BSD License" (see
<strong>docs/LICENSE.TXT
</strong> in the BareMetal OS distribution). Essentially, it means you can do anything you like with the code, including basing your own project on it, providing you retain the license file and give credit to Return Infinity and the BareMetal OS developers for their work.
</p>